const app = getApp()
import {
        getCart,
        del,
        add,
        update
} from '../../../api/cart'

import {
        list,
        find
} from '../../../api/product'

const util = require('../../../utils/util.js')

Page({

        /**
         * 页面的初始数据
         */
        data: {
                haveProduct: false, //判断购物车是否为空
                isShowBottomFrame: false, //底部属性弹窗的开关
                productIdToBottomFrame: "", //暂存被点击要更改属性的商品id
                deleteOff: false,
                productSelectedOff: false,
                allchecked: "", //全选的控制器
                sum: 0, //消费总额
                selectList: [],
                selectShopList: [],
                selectProductList: [],
                shopList: [], //商户列表，里面的productList是商品列表
                cartInfo: [], //购物车商品信息
                //失效的商品
                haveInvaild: false, //判断是否有失效商品
                showInvaildProduct: true,
                invalidProductList: [
                        // {
                        //         productId: 15,
                        //         checked: "",
                        //         productSrc: "http://cdn.rzico.com/v4.0/test.png",
                        //         title: "人体工学电脑椅 家用 多功能护腰办公椅子老板椅 转椅网布椅转椅网布椅",
                        //         msg: "商品已下架"
                        // },
                        // {
                        //         productId: 16,
                        //         checked: "",
                        //         productSrc: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
                        //         title: "人体工学电脑椅 家用 多功能护腰办公椅子老板椅 转椅网布椅转椅网布椅",
                        //         msg: "商品已断货"
                        // },
                        // {
                        //         productId: 17,
                        //         checked: "",
                        //         productSrc: "http://cdn.rzico.com/v4.0/footprint-redWine.jpg",
                        //         title: "人体工学电脑椅 家用 多功能护腰办公椅子老板椅 转椅网布椅转椅网布椅",
                        //         msg: "商品已下架"
                        // }
                ]
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function(options) {

        },

        /**
         * 生命周期函数--监听页面初次渲染完成
         */
        onReady: function() {

        },

        /**
         * 生命周期函数--监听页面显示
         */
        onShow: function() {
                this.cartInfoRequest((data) => {
                        this.formateCartData(data)
                })
        },

        //请求购物车数据的函数
        cartInfoRequest(callback) {
                let data = {}
                data.uuid = app.globalData.uuid
                console.info("app.globalData.token--" + app.globalData.token)
                console.info("data.uuid--" + data.uuid)
                getCart(data).then(
                        res => {
                                console.log("发送购物的数据请求--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        this.data.cartInfo = res.data.cart.itemVoList
                                        // console.log("发送购物的数据请求cartInfo--" + JSON.stringify(this.data.cartInfo))
                                        if (callback) {
                                                callback(res.data.cart.itemVoList)
                                        }
                                        // wx.navigateTo({
                                        //         url: '/pages/goods/submit/index',
                                        //         success: function (res) { },
                                        //         fail: function (res) { },
                                        //         complete: function (res) { },
                                        // })
                                }
                        }
                ).catch(error => {
                        console.log(error)
                })
        },

        //整理购物车数据
        formateCartData(data) {
                let shopList = []
                let invalidProductList = []
                let haveInvaild = this.data.haveInvaild
                let haveProduct = this.data.haveProduct
                console.info("商品数据data" + JSON.stringify(data))
                if (data.length > 0) {
                        let shopIdList = []
                        data.map((item) => {
                                if (item.stock == 0 || item.isMarketable == false){
                                        haveInvaild = true
                                        let invalidProductListItem = {}
                                        invalidProductListItem.id = item.id
                                        invalidProductListItem.productId = item.productId
                                        invalidProductListItem.goodsId = item.goodsId
                                        invalidProductListItem.brandId = item.brandId
                                        invalidProductListItem.productCategoryId = item.productCategoryId
                                        invalidProductListItem.thumbnail = util.thumbnail(item.thumbnail, 148, 148)
                                        // shopListItem.enterpriseLogo = util.thumbnail(item.enterpriseLogo, 44, 44)
                                        invalidProductListItem.name = item.name
                                        invalidProductListItem.tag = item.spec1 + " " + item.spec2
                                        invalidProductListItem.price = item.price
                                        invalidProductListItem.checked = item.chk
                                        invalidProductListItem.point = item.point
                                        invalidProductListItem.quantity = item.quantity
                                        invalidProductListItem.stock = item.stock
                                        invalidProductListItem.isMarketable = item.isMarketable
                                        if (item.stock == 0) {
                                                invalidProductListItem.msg = "缺货中"
                                        }
                                        if (item.isMarketable == false) {
                                                invalidProductListItem.msg = "商品已经下架了"
                                        }
                                        invalidProductList.push(invalidProductListItem)
                                }else{
                                        shopIdList.push(item.enterpriseId)
                                }
                        })
                        shopIdList = Array.from(new Set(shopIdList));
                        console.info("invalidProductList" + JSON.stringify(invalidProductList))
                        if (shopIdList.length > 0) {
                                shopIdList.map((item) => {
                                        let shopListItem = {}
                                        shopListItem.enterpriseId = item
                                        shopListItem.productList = []
                                        shopListItem.checked = false
                                        // shopListItem.enterpriselogo = "http://cdn.rzico.com/v4.0/classify-ts.png" //这个后面要调整到下面的循环中
                                        data.map((item1) => {
                                                if (item1.stock == 0 || item1.isMarketable == false){
                                                        // haveInvaild = true
                                                        // let invalidProductListItem = {}
                                                        // invalidProductListItem.id = item1.id
                                                        // invalidProductListItem.productId = item1.productId
                                                        // invalidProductListItem.goodsId = item1.goodsId
                                                        // invalidProductListItem.brandId = item1.brandId
                                                        // invalidProductListItem.productCategoryId = item1.productCategoryId
                                                        // invalidProductListItem.thumbnail = util.thumbnail(item1.thumbnail, 148, 148)
                                                        // shopListItem.enterpriseLogo = util.thumbnail(item1.enterpriseLogo, 44, 44)
                                                        // invalidProductListItem.name = item1.name
                                                        // invalidProductListItem.tag = item1.spec1 + " " + item1.spec2
                                                        // invalidProductListItem.price = item1.price
                                                        // invalidProductListItem.checked = item1.chk
                                                        // invalidProductListItem.quantity = item1.quantity
                                                        // invalidProductListItem.stock = item1.stock
                                                        // invalidProductListItem.isMarketable = item1.isMarketable
                                                        // if (item1.stock == 0){
                                                        //         invalidProductListItem.msg = "缺货中"
                                                        // }
                                                        // if (item1.isMarketable == false) {
                                                        //         invalidProductListItem.msg = "商品已经下架了"
                                                        // }
                                                        // invalidProductList.push(invalidProductListItem)
                                                }else{
                                                        haveProduct = true
                                                        shopListItem.enterpriseName = item1.enterpriseName
                                                        let productListItem = {}
                                                        productListItem.id = item1.id
                                                        productListItem.productId = item1.productId
                                                        productListItem.goodsId = item1.goodsId
                                                        productListItem.brandId = item1.brandId
                                                        productListItem.productCategoryId = item1.productCategoryId
                                                        productListItem.thumbnail = util.thumbnail(item1.thumbnail, 148, 148)
                                                        shopListItem.enterpriseLogo = util.thumbnail(item1.enterpriseLogo, 44, 44)
                                                        productListItem.name = item1.name
                                                        productListItem.tag = item1.spec1?item1.spec1:"" + " " + item1.spec2?item1.spec2:""
                                                        productListItem.price = item1.price
                                                        productListItem.checked = item1.chk
                                                        productListItem.point = item1.point
                                                        productListItem.minLimit = item1.minLimit
                                                        productListItem.maxLimit = item1.maxLimit
                                                        productListItem.quantity = item1.quantity
                                                        productListItem.stock = item1.stock
                                                        shopListItem.productList.push(productListItem)
                                                }

                                        })
                                        shopList.push(shopListItem)
                                })
                                console.info("商户列表invalidProductList" + JSON.stringify(invalidProductList))
                                console.info("商户列表shopList" + JSON.stringify(shopList))
                                if (shopList.length > 0) {
                                        shopList.map((item) => {
                                                        let shopOff = true //用来检查该供应商下面的商品是否被全选
                                                        if (item.productList.length > 0) {
                                                                item.productList.map((item1) => {
                                                                        if (item1.checked == false || item1.checked == "") {
                                                                                shopOff = false
                                                                        }
                                                                })
                                                        }
                                                        if (shopOff) {
                                                                item.checked = true
                                                        }
                                        })
                                }
                        }
                        this.setData({
                                invalidProductList: invalidProductList,
                                shopList: shopList,
                                haveInvaild: haveInvaild,
                                haveProduct: haveProduct
                        })
                        this.calculate()
                }else{
                        this.setData({
                                invalidProductList: [],
                                shopList: [],
                                haveInvaild: false,
                                haveProduct: false
                        })
                }
        },

        //店铺的全选按钮触发事件
        shopCheck: function(e) {
                let enterpriseId = e.target.dataset.shopid
                let shopState = e.target.dataset.shopstate
                let cartInfo = this.data.shopList
                if (shopState == "" || shopState == false) {
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        if (item.enterpriseId == enterpriseId) {
                                                item.checked = true
                                                if (item.productList.length > 0) {
                                                        item.productList.map((item1) => {
                                                                item1.checked = true
                                                        })
                                                }
                                        }
                                })
                        }
                } else {
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        if (item.enterpriseId == enterpriseId) {
                                                item.checked = false
                                                if (item.productList.length > 0) {
                                                        item.productList.map((item1) => {
                                                                item1.checked = false
                                                        })
                                                }
                                        }
                                })
                        }

                }
                this.setData({
                        shopList: cartInfo
                })
                this.calculate()
        },

        //单件商品的选中事件
        productCheck: function(e) {
                let enterpriseId = e.target.dataset.shopid
                let productId = e.target.dataset.productid
                let productState = e.target.dataset.productstate
                let cartInfo = this.data.shopList
                if (productState == "" || productState == false) {
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        if (item.enterpriseId == enterpriseId) {
                                                let shopOff = true //用来检查该供应商下面的商品是否被全选
                                                // item.checked = true
                                                if (item.productList.length > 0) {
                                                        item.productList.map((item1) => {
                                                                if (item1.productId == productId) {
                                                                        item1.checked = true
                                                                }
                                                                if (item1.checked == false || item1.checked == "") {
                                                                        shopOff = false
                                                                }
                                                        })
                                                }
                                                if (shopOff) {
                                                        item.checked = true
                                                }
                                        }
                                })
                        }
                } else {
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        if (item.enterpriseId == enterpriseId) {
                                                item.checked = false
                                                if (item.productList.length > 0) {
                                                        item.productList.map((item1) => {
                                                                if (item1.productId == productId) {
                                                                        item1.checked = false
                                                                }
                                                        })
                                                }
                                        }
                                })
                        }

                }
                this.setData({
                        shopList: cartInfo
                })
                this.calculate()
        },

        //全选的事件被触发
        allCheck: function(e) {
                console.info("全选按钮被点击--" + e.target.dataset.state)
                let state = e.target.dataset.state
                let cartInfo = this.data.shopList
                if (state == "" || state == false) {
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        item.checked = true
                                        if (item.productList.length > 0) {
                                                item.productList.map((item1) => {
                                                        item1.checked = true
                                                })
                                        }
                                })
                                state = true
                        }
                } else {
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        item.checked = false
                                        if (item.productList.length > 0) {
                                                item.productList.map((item1) => {
                                                        item1.checked = false
                                                })
                                        }
                                })
                                state = false
                        }

                }
                this.setData({
                        shopList: cartInfo,
                        allchecked: state
                })
                this.calculate()
        },

        //统计目前有多少个商品被选中
        calculate: function() {
                let selectShopList = []
                let selectProductList = []
                let sum = 0
                let allchecked = true
                if (this.data.shopList.length > 0) {
                        this.data.shopList.map((item) => {
                                if (item.checked == true) {
                                        selectShopList.push(item.enterpriseId)
                                }else{
                                        allchecked = false
                                }
                                if (item.productList.length > 0) {
                                        item.productList.map((item1) => {
                                                let selectProductListItem = {}
                                                if (item1.checked == true) {
                                                        selectProductListItem.productId = item1.productId
                                                        selectProductListItem.id = item1.id
                                                        selectProductList.push(selectProductListItem)
                                                        sum += item1.price * item1.quantity
                                                }
                                        })
                                }
                        })
                }
                console.info("目前被选中的商户id--" + selectShopList)
                console.info("目前被选中的商品id--" + JSON.stringify(selectProductList))
                this.setData({
                        sum: sum.toFixed(2),
                        selectShopList: selectShopList,
                        selectProductList: selectProductList,
                        allchecked: allchecked
                })

                app.event.emit("onCartChange");
        },

        //数量减少事件
        numReduce: function(e) {
                console.log("单间商品被点击，e==" + JSON.stringify(e))
                console.log("单间商品被点击，enterpriseId==" + JSON.stringify(e.target.dataset.enterpriseid))
                console.log("单间商品被点击，productid==" + JSON.stringify(e.target.dataset.productid))
                console.log("单间商品被点击，currentNum==" + JSON.stringify(e.target.dataset.currentnum))
                console.log("minlimit==" + JSON.stringify(e.target.dataset.minlimit))
                let enterpriseId = e.target.dataset.enterpriseid
                let productId = e.target.dataset.productid
                let currentNum = e.target.dataset.currentnum
                let minLimit = e.target.dataset.minlimit
                let cartInfo = this.data.shopList
                if (minLimit > 0 && currentNum <=  minLimit){
                        wx.showToast({
                                title: '商品数量不能再少了呦！',
                                icon: 'none',
                                duration: 1500
                        })
                }else if (currentNum <= 1 ) {
                        wx.showToast({
                                title: '商品数量不能再少了呦！',
                                icon: 'none',
                                duration: 1500
                        })
                } else {
                        currentNum--
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        if (item.enterpriseId == enterpriseId) {
                                                if (item.productList.length > 0) {
                                                        item.productList.map((item1) => {
                                                                if (item1.productId == productId) {
                                                                        item1.quantity = currentNum
                                                                        item1.checked = true
                                                                        let data = []
                                                                        let dataItem = {}
                                                                        dataItem.id = item1.id
                                                                        dataItem.quantity = currentNum
                                                                        dataItem.chk = true
                                                                        data.push(dataItem)
                                                                        this.cartUpdate(data, ()=>{
                                                                                this.setData({
                                                                                        shopList: cartInfo
                                                                                })
                                                                                this.calculate()
                                                                        })
                                                                }
                                                        })
                                                }
                                        }
                                })
                        }

                }

        },

        //数量增加事件
        numAdd: function(e) {
                console.log("单间商品被点击，shopid==" + JSON.stringify(e.target.dataset.shopid))
                console.log("单间商品被点击，productid==" + JSON.stringify(e.target.dataset.productid))
                console.log("单间商品被点击，currentNum==" + JSON.stringify(e.target.dataset.currentnum))
                console.log("单间商品被点击，maxlimit==" + JSON.stringify(e.target.dataset.maxlimit))
                console.log("stock==" + JSON.stringify(e.target.dataset.stock))

                let enterpriseId = e.target.dataset.enterpriseid
                let productId = e.target.dataset.productid
                let currentNum = e.target.dataset.currentnum
                let maxlimit = e.target.dataset.maxlimit
                let stock = e.target.dataset.stock
                let cartInfo = this.data.shopList
                if (currentNum >= maxlimit && maxlimit > 0) {
                        wx.showToast({
                                title: '商品数量已达限购呦！',
                                icon: 'none',
                                duration: 1500
                        })
                } else if (currentNum >= stock){
                        wx.showToast({
                                title: '商品数量已达上限呦！',
                                icon: 'none',
                                duration: 1500
                        })
                }else {
                        currentNum++
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        if (item.enterpriseId == enterpriseId) {
                                                if (item.productList.length > 0) {
                                                        item.productList.map((item1) => {
                                                                if (item1.productId == productId) {
                                                                        item1.quantity = currentNum
                                                                        item1.checked = true
                                                                        let data = []
                                                                        let dataItem = {}
                                                                        dataItem.id = item1.id
                                                                        dataItem.quantity = currentNum
                                                                        dataItem.chk = true
                                                                        data.push(dataItem)
                                                                        this.cartUpdate(data, () => {
                                                                                this.setData({
                                                                                        shopList: cartInfo
                                                                                })
                                                                                this.calculate()
                                                                        })
                                                                }
                                                        })
                                                }
                                        }
                                })
                        }
                }
        },

        //数量直接编辑的弹窗事件
        numEdit: function(e) {
                console.log("单间商品被点击，shopid==" + JSON.stringify(e.target.dataset.shopid))
                console.log("单间商品被点击，productid==" + JSON.stringify(e.target.dataset.productid))
                console.log("单间商品被点击，currentNum==" + JSON.stringify(e.target.dataset.currentnum))
                let enterpriseId = e.target.dataset.shopid
                let productId = e.target.dataset.productid
                let currentNum = e.target.dataset.currentnum
        },

        //数量输入框的失焦事件
        bindblur: function(e) {
                let cartInfo = this.data.shopList
                let enterpriseId = e.target.dataset.enterpriseid
                let productId = e.target.dataset.productid
                let currentNum = e.detail.value
                let stock = e.target.dataset.stock
                let maxLimit = e.target.dataset.maxlimit
                let minLimit = e.target.dataset.minlimit
                console.log("单间商品被点击，minLimit==" + minLimit)
                console.log("单间商品被点击，maxLimit==" + maxLimit)
                console.log("单间商品被点击，stock==" + stock)
                if (currentNum < minLimit && minLimit != 0){
                        wx.showToast({
                                title: '商品数量超过下限呦！',
                                icon: 'none',
                                duration: 1500
                        })
                        this.setData({
                                shopList: cartInfo
                        })
                } else if (currentNum < 1){
                        wx.showToast({
                                title: '商品数量不能小于1呦！',
                                icon: 'none',
                                duration: 1500
                        })
                        this.setData({
                                shopList: cartInfo
                        })
                }else if (currentNum > maxLimit && maxLimit != 0) {
                        wx.showToast({
                                title: '商品数量超过上限呦！',
                                icon: 'none',
                                duration: 1500
                        })
                        this.setData({
                                shopList: cartInfo
                        })
                } else if (currentNum > stock){
                        wx.showToast({
                                title: '商品数量超过上限呦！',
                                icon: 'none',
                                duration: 1500
                        })
                        this.setData({
                                shopList: cartInfo
                        })
                }else {
                        if (cartInfo.length > 0) {
                                cartInfo.map((item) => {
                                        if (item.enterpriseId == enterpriseId) {
                                                if (item.productList.length > 0) {
                                                        item.productList.map((item1) => {
                                                                if (item1.productId == productId) {
                                                                        item1.quantity = currentNum
                                                                        item1.checked = true
                                                                        let data = []
                                                                        let dataItem = {}
                                                                        dataItem.id = item1.id
                                                                        dataItem.quantity = currentNum
                                                                        dataItem.chk = true
                                                                        data.push(dataItem)
                                                                        this.cartUpdate(data, () => {
                                                                                this.setData({
                                                                                        shopList: cartInfo
                                                                                })
                                                                                this.calculate()
                                                                        })
                                                                }
                                                        })
                                                }
                                        }
                                })
                        }
                }
        },

        //跳转商品详情页
        toProductDetail: function(e) {
                let id = e.currentTarget.dataset.productid
                wx.navigateTo({
                        url: '/pages/goods/product/details?id=' + id,
                })
        },

        //调用购物车的数量更新接口
        cartUpdate(data, callback) {
                update(data).then(
                        res => {
                                console.log("购物车数量变更请求的请求--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        if (callback) {
                                                callback()
                                        }
                                }
                        }
                ).catch(error => {
                        console.log(error)
                        wx.showToast({
                                title: '服务器开小差了，请重试',
                                icon: 'none',
                                duration: 2000
                        })
                })
        },

        //调用购物车的添加接口
        cartAdd(productId, num, callback) {
                //发送添加购物车的请求
                let data = {}
                data.productId = productId
                data.promotionId = ""
                data.quantity = num
                data.uuid = app.globalData.uuid
                console.log("商品加入购物车的请求data--" + JSON.stringify(data))
                add(data).then(
                        res => {
                                console.log("商品加入购物车的请求--" + JSON.stringify(res))
                                if (res.type == 'success') {
                                        if (callback) {
                                                callback(res.data)
                                        }
                                }
                        }
                ).catch(error => {
                        console.log(error)
                        wx.showToast({
                                title: '服务器开小差了，请重新选择',
                                icon: 'none',
                                duration: 2000
                        })
                })
        },

        //调用购物车的删除的接口
        cartDelete(data, type, callback) {
                console.info("选中的商品列表selectShopList--" + JSON.stringify(data))
                let ids = ""
                let idList = []
                if (data.length > 0) {
                        idList = []
                        data.map((item) => {
                                idList.push(item.id)
                        })
                        ids = idList.join(',')
                        del(ids).then(
                                res => {
                                        console.log("购物车删除商品的请求--" + JSON.stringify(res))
                                        if (res.type == 'success') {
                                                let shopList = this.data.shopList
                                                let shopIndex = ""
                                                let productIndex = ""
                                                if (type == "del"){
                                                        data.map((item, index) => {
                                                                if (shopList.length > 0) {
                                                                        shopList.map((item1, index1) => {
                                                                                if (item1.productList.length > 0) {
                                                                                        item1.productList.map((item2, index2) => {
                                                                                                if (item.id == item2.id) {
                                                                                                        shopIndex = index1
                                                                                                        productIndex = index2
                                                                                                }
                                                                                        })
                                                                                }
                                                                        })
                                                                        shopList[shopIndex].productList.splice(productIndex, 1)
                                                                }
                                                                if (shopList[shopIndex].productList.length == 0){
                                                                        shopList.splice(shopIndex, 1)
                                                                }

                                                        })
                                                        let haveProduct =this.data.haveProduct
                                                        if(shopList.length == 0){
                                                                haveProduct = false
                                                        }
                                                        this.setData({
                                                                shopList: shopList,
                                                                haveProduct:haveProduct
                                                        })
                                                }else if(type == "change"){
                                                        if (callback) {
                                                                callback()
                                                        }
                                                }


                                                app.event.emit("onCartChange");


                                        }


                                }
                        ).catch(error => {
                                console.log(error)
                                wx.showToast({
                                        title: '服务器开小差了，请重新添加商品',
                                        icon: 'none',
                                        duration: 2000
                                })
                        })
                }
        },

        //删除选中商品
        toDelete: function(e) {
                let _this = this
                let num = this.data.selectProductList.length
                if (num > 0) {
                        wx.showModal({
                                title: '温馨提示',
                                content: '您确定要删除' + num + '件商品',
                                success(res) {
                                        if (res.confirm) {
                                                if (num > 0) {
                                                        _this.cartDelete(_this.data.selectProductList, "del")
                                                }
                                        }
                                }
                        })
                } else {
                        wx.showToast({
                                title: '未选择要删除的商品',
                                icon: 'none',
                                duration: 1500
                        })
                }
        },

        //清空失效商品
        clearInvaild: function(e) {
                let _this = this
                wx.showModal({
                        title: '温馨提示',
                        content: '您确定清空失效商品',
                        success(res) {
                                if (res.confirm) {
                                        let idsList = []
                                        let ids = ""
                                        if (_this.data.invalidProductList.length > 0) {
                                                _this.data.invalidProductList.map((item)=>{
                                                        idsList.push(item.id)
                                                })
                                                ids = idsList.join(",")
                                        }
                                        del(ids).then(
                                                res => {
                                                        console.log("购物车删除商品的请求--" + JSON.stringify(res))
                                                        if (res.type == 'success') {
                                                                _this.setData({
                                                                        invalidProductList:[],
                                                                        haveInvaild:false
                                                                })
                                                        }
                                                }
                                        ).catch(error => {
                                                console.log(error)
                                                wx.showToast({
                                                        title: '服务器开小差了，请重新添加商品',
                                                        icon: 'none',
                                                        duration: 2000
                                                })
                                        })
                                }
                        }
                })
        },

        //跳转订单确认页面
        productSumbit: function(e) {
                let _this = this
                let selectList = []
                this.cartInfoRequest((data) => {
                        let sendData = []
                        let shopList = this.data.shopList
                        console.info("shopList--" + JSON.stringify(shopList))
                        if (data.length > 0) {
                                data.map((item) => {
                                        let sendDataItem = {}
                                        if (shopList.length > 0) {
                                                shopList.map((item1) => {
                                                        if (item1.productList.length > 0) {
                                                                item1.productList.map((item2) => {
                                                                        if (item.id == item2.id) {
                                                                                sendDataItem.id = item.id
                                                                                sendDataItem.chk = item2.checked
                                                                                sendDataItem.quantity = item2.quantity
                                                                                sendData.push(sendDataItem)
                                                                                if (item2.checked){
                                                                                        selectList.push(item.id)
                                                                                }
                                                                        }
                                                                })
                                                        }
                                                })
                                        }

                                })
                        }
                        console.info("sendData--" + JSON.stringify(sendData))
                        console.info("selectList--" + JSON.stringify(selectList))
                        if (selectList.length > 0){
                                _this.cartUpdate(sendData, () => {
                                        wx.navigateTo({
                                                url: '/pages/goods/submit/index'
                                        })
                                })
                        }else{
                                wx.showToast({
                                        title: '请选择需要的商品',
                                        icon:"none",
                                        duration:2000
                                })
                        }

                })
        },

        //打开属性选择弹窗
        showBottomFrame: function(e) {
                let productIdToBottomFrame = e.currentTarget.dataset.productid
                this.setData({
                        productIdToBottomFrame: productIdToBottomFrame
                })
                console.info("被选中的productIdToBottomFrame" + JSON.stringify(productIdToBottomFrame))
                this.selectComponent('#bottomFrame').showFrame(productIdToBottomFrame, "changeAttr");
        },

        //属性选择弹窗完成后数据回传函数
        selectedInfo(data) {
                let _this = this
                console.info("被选中要替换的商品--" + this.data.productIdToBottomFrame)
                console.info("被选中要替换的商品--" + JSON.stringify(this.data.shopList))
                console.info("属性数据回收了--" + JSON.stringify(data))
                let shopList = this.data.shopList
                let productoff = true //查询所选的商品是不是已经存在
                let shopIndex = ""
                let productIndex = ""
                let selectProductList = []
                if (shopList.length > 0) {
                        shopList.map((item, index) => {
                                if (item.productList.length > 0) {
                                        item.productList.map((item1, index1) => {
                                                if (item1.productId == data.detail.productId) {
                                                        productoff = false
                                                        console.info("被选中要替换的商品item1--" + JSON.stringify(item1))
                                                        if (item1.maxLimit == 0  ){
                                                                item1.quantity += data.detail.currentNum
                                                        } else if (item1.quantity + data.detail.currentNum < item1.maxLimit){
                                                                item1.quantity += data.detail.currentNum
                                                        }else{
                                                                wx.showToast({
                                                                        title: '商品数量超过上限',
                                                                        icon: "none",
                                                                        duration: 1500
                                                                })
                                                        }
                                                }
                                                if (this.data.productIdToBottomFrame == item1.productId) {
                                                        console.info("被选中要替换的商品item1--" + JSON.stringify(item1))
                                                        shopIndex = index
                                                        productIndex = index1
                                                        let selectProductListItem = {}
                                                        selectProductListItem.id = item1.id
                                                        selectProductListItem.productId = item1.productId
                                                        selectProductList.push(selectProductListItem)
                                                }
                                        })
                                }
                        })
                        _this.setData({
                                shopList: shopList
                        })
                        console.info("被选中的selectProductList--" + JSON.stringify(selectProductList))
                        if (this.data.productIdToBottomFrame != data.detail.productId) {
                                _this.cartDelete(selectProductList, "change", () => {
                                        if (productoff) {
                                                this.cartAdd(data.detail.productId, data.detail.currentNum, (res) => {
                                                        console.log("属性替换成功后的返回res--" + JSON.stringify(res))
                                                        let productListItem = {}
                                                        productListItem.id = res.id
                                                        productListItem.productId = res.productId
                                                        productListItem.goodsId = res.goodsId
                                                        productListItem.brandId = res.brandId
                                                        productListItem.productCategoryId = res.productCategoryId
                                                        productListItem.thumbnail = res.thumbnail
                                                        productListItem.name = res.name
                                                        productListItem.point = res.point
                                                        productListItem.tag = res.spec1?res.spec1:'' + " " + res.spec2?res.spec2:''
                                                        productListItem.price = res.price
                                                        productListItem.checked = res.chk
                                                        productListItem.quantity = res.quantity
                                                        console.log("单件商品数据productListItem--" + JSON.stringify(productListItem))
                                                        shopList[shopIndex].productList.splice(productIndex, 1, productListItem)
                                                        console.log("shopList--" + JSON.stringify(shopList))
                                                        _this.setData({
                                                                shopList: shopList
                                                        })
                                                })
                                        }
                                })
                        }
                }

        },

        //空购物车时，跳转到首页
        toIndex() {
                wx.switchTab({
                        url: '/pages/index/index'
                })
        }
})
